package com.zensation.ticklemepanda.util;

import com.zensation.ticklemepanda.definition.GlobalDefine;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * MeyClubSharedPreferences class which saves setting values
 * 
 * @author Lemon
 */
public class TickleMePandaSharedPreference {

	public String TAG = getClass().getSimpleName();

	public static final String SCORE = "SCORE";
	public static final String PLAY_AGAIN = "PLAY_AGAIN";
	public static final String EXIT_GAME = "EXIT_GAME";
	public static final String GIGGLE_BY_TAP = "GIGGLE_BY_TAP";
	public static final String DENSITY ="DENSITY";

	// ================================================================

	public static TickleMePandaSharedPreference instance;

	public Context context;

	public SharedPreferences preferences;

	public TickleMePandaSharedPreference(Context contextPre) {
		this.context = contextPre;
		preferences = context.getSharedPreferences("tickmepanda", 0);
	}

	public TickleMePandaSharedPreference() {
	}

	/**
	 * Constructor
	 * 
	 * @param context
	 * @return
	 */
	public static TickleMePandaSharedPreference getInstance(Context context) {
		if (instance == null) {
			instance = new TickleMePandaSharedPreference();
			instance.context = context;
		}
		return instance;
	}

	// ======================== CORE FUNCTIONS ========================

	/**
	 * Save score to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void putIntValue(String key, int n) {
		// SmartLog.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(key, n);
		editor.commit();
	}

	/**
	 * Read score to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public int getIntValue(String key) {
		// SmartLog.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		return pref.getInt(key, 0);
	}

	/**
	 * set play again to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void setPlayAgain(String key, boolean b) {
		// SmartLog.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, b);
		editor.commit();
	}

	/**
	 * get play again to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getPlayAgainValue(String key) {
		// SmartLog.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		return pref.getBoolean(key, false);
	}

	/**
	 * set exit game to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void setExitGame(String key, boolean b) {
		// SmartLog.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, b);
		editor.commit();
	}

	/**
	 * get play again to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getExitGameValue(String key) {
		// SmartLog.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		return pref.getBoolean(key, false);
	}

	public void putScoreValue(int score) {
		putIntValue(SCORE, score);
	}

	public int getScoreValue() {
		return getIntValue(SCORE);
	}

	public void setPlayAgain(boolean b) {
		setPlayAgain(PLAY_AGAIN, b);
	}

	public boolean getPlayAgain() {
		return getPlayAgainValue(PLAY_AGAIN);
	}

	public void setExitGame(boolean b) {
		setExitGame(EXIT_GAME, b);
	}

	public boolean getExitGame() {
		return getExitGameValue(EXIT_GAME);
	}

	/**
	 * set exit game to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void setGiggleByTap(String key, boolean b) {
		// SmartLog.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, b);
		editor.commit();
	}

	/**
	 * get play again to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getGiggleByTapPanda(String key) {
		// SmartLog.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		return pref.getBoolean(key, false);
	}

	public void setGiggleByTap(boolean b) {
		setGiggleByTap(GIGGLE_BY_TAP, b);
	}

	public boolean getGiggleByTap() {
		return getGiggleByTapPanda(GIGGLE_BY_TAP);
	}

	/**
	 * Save density to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void putDensityValue(String key, int n) {
		// SmartLog.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(key, n);
		editor.commit();
	}

	/**
	 * Read density of device to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public int getDensityValue(String key) {
		// SmartLog.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				GlobalDefine.TICKLE_ME_PANDA_PREFERENCES, 0);
		return pref.getInt(key, 0);
	}
	
	public void putDensityValue(int score) {
		putIntValue(DENSITY, score);
	}

	public int getDensityValue() {
		return getIntValue(DENSITY);
	}
}
